package cn.gok.service.Impl;

import cn.gok.mapper.UserMapper;
import cn.gok.pojo.Dentity;
import cn.gok.pojo.User;
import cn.gok.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public User login(String username,String password) {
        User user=userMapper.login(username,password);
        if(user!=null){
            user.setDentity(userMapper.seldentity(user.getDentity_id()));
        }
        return user;
    }

    @Override
    public List<String> link() {
        return userMapper.link();
    }

    @Override
    public int registered(String username, String password) {
        int code= 0;
        try {
            code = userMapper.registered(username,password);
        } catch (Exception e) {
            e.printStackTrace();
//           添加失败，查询是否存在
            List<User> tp=userMapper.selof(username);
            if(tp!=null){
//                账号已存在
                code=3;
            }
        }

        return code;
    }

    @Override
    public List<User> selof(String query) {
        List<User> user=userMapper.selof(query);
        for (User u:user) {
            if(!u.getDentity_id().equals("")){
                u.setDentity(userMapper.seldentity(u.getDentity_id()));
            }
        }
        return user;
    }

    @Override
    public List<User> selall() {
        return userMapper.selall();
    }

    @Override
    public List<Dentity> selalldentity() {
        return userMapper.selalldentity();
    }

    @Override
    public int del(String query) {
        return userMapper.del(query);
    }

    @Override
    public int change(User user) {
        return userMapper.change(user);
    }
}
